<?php

namespace app\api\controller;

use app\common\model\Enum;
use app\api\model\WxappPage;
use think\Db;
use think\facade\Validate;
use app\api\model\HttpMachine;
use think\facade\Request;

/**
 * 首页控制器
 * Class Index
 * @package app\api\controller
 */
class Index extends Controller
{
    /**
     * 首页diy数据 http://www.cai.com/index.php/api/index/page.html
     * @return array
     * @throws \think\exception\DbException
     */
 
    public function cardWash2()
    {
        return $this->renderSuccess_200("success");
    }

    public function cardWash3()
    {
        return $this->renderError_400("success");
    }
    
    public function test2()
    {
        $err_code=200;
        
        echo $err_code;
        exit();
    }

    public function test()
    {
        $post = $this->request->post();
        $map=[
        ];
        $list=Db::table('cardwash_log')->where($map)->order('id', 'desc')->limit(10)->select();
        echo '<html><head><meta name="viewport" content="width=device-width, initial-scale=0.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" />
        </head><body>10条刷卡历史:<table border="1" cellspacing="0" cellpadding="1"><tr><th>卡号</th><th>机器码</th><th>命令</th><th>结果</th><th>时间</th></tr>';
        foreach($list as $k => $v) {
            $card_no = $v['card_no'];
            $machine_no = $v['machine_no'];
            $action = $v['action'];
            $msg = $v['msg'];
            $create_at = $v['create_at'];
            echo "<tr><td>$card_no</td><td>$machine_no</td><td>$action</td><td>$msg</td><td>$create_at</td></tr>";
        }

        echo "</table></body></html>";
exit();
        // return $this->renderSuccess(compact('list'));

    }
    

    public  function curl_file_get_contents($durl){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $durl);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $r = curl_exec($ch);
        curl_close($ch);
        return $r;
     }

    public function old2new()
    {
        // set_time_limit(0);
        // //这里很重要,就要去新的系统去同步你的账号信息
        // $data_old = Db::table('user')->select();
        // foreach ($data_old as $row){
        //    // $code =  self::GetCardMax();
        //     $uid=$row["id"]; 
            
           
        //     $amount =  $row["amount"];

        //     //同步用户现在的金额
        //     if($amount >0)
        //     {
        //         $no = Enum::orderNo(Enum::order_type_cscz);
        //         $time =date('Y-m-d H:i:s',time());
    
        //         $data=[
        //             'user_id' => $uid,
        //             'open_id' => $row['open_id'],
        //             'union_id' => $row['union_id'],
        //             'type' => Enum::b_type_cscz,
        //             'trade_no' => "",
        //             'card_no' => "",
        //             'order_no' => $no,
        //             'apsid' => "",
        //             'pay_status' => Enum::PAY_SUCESS,
        //             'create_at' => $time,
        //             'update_at' => $time,
        //             'czamount' => $amount,
        //             'amount' => $amount,
        //             'gift' => 0
        //         ];
    
        //         //插入数据
        //         $book=[
        //             'user_id' => $uid,
        //             'b_type' => Enum::b_type_cscz,
        //             'b_id' => 0,
        //             'b_no' => $no,
        //             'create_at' => $time,
        //             'old_amount' =>0,
        //             'var_amount' =>$amount ,
        //             'amount' => $amount
        //         ];
        //     }
    
             
        //         if($amount >0)
        //         {
        //             $id=Db::table('charge')->insertGetId($data);
        //             $book['b_id'] =$id;
        //             Db::table('daybook')->strict(false)->insert($book);
        //         }
        //         //字段变化
        //        // Db::table('user')->where('id',$uid)->update([ 'code'=> $code]);
        // }
          
        return $this->renderSuccess(1); 
    }


    public function GetCardMax(){
        $number_max =Db::table('number_max')->where("id",1)->find();
        if(empty($number_max)){
            $maxcard = 11000;//初始
            $data=array(
                'maxnum'=> $maxcard
            );
            Db::table('number_max')->strict(false)->insertGetId($data);
        }else{
            $maxcard = $number_max['maxnum'];
        }
    
        $strMaxcardid = $maxcard + 1;
        $ar = str_split($strMaxcardid);
        foreach($ar as $k => $v) {
            if ($v == 4 ) {
                $ar[$k] ++;
                break;
            }
        }
        $strMaxcardid = join('', $ar);
        $maxcard = $strMaxcardid;
        Db::table('number_max')->where('id',1)->update(['maxnum' => $maxcard]);
        return  $maxcard;
    }


	public function page()
    {
		//  //设置post数据
        //         $params = array(
        //             "action" => "workstatus",
        //             "machineSn" => "254155",
        //             "sign" => "9AD1DAC49AAF4E362048F7C84F6454DE",
        //             "timeStamp" => 1556867496,
        //             "token" => "e21edcc33d4141e1a43d4a2611bf35a4"
        //         );

        //         $sumbit_url="http://v1.webapi.cooleasy.net/WasherApi";
        //         $re = HttpMachine::send_post($sumbit_url,$params);
        //         echo $re;
        // exit();

        // http://127.0.0.1:8080/wash/machine/queryWashTypeAndPrice?machineNo=254155
        //         echo md25("olxJN5Z-1105FKhXILDyQ8y9_ceY");
        // exit();
                // echo strtoupper(md5("GFdsCY5T56uzTiE0zFka3fsmLCdFwjbfaction=workstatusmachinesn=254155timestamp=1556865334token=e21edcc33d4141e1a43d4a2611bf35a4"));

                //echo strtoupper(md5("olxJN5Q9olPpCQsM4Eb1oMbcYRws"));
            
        //         $re = HttpMachine::GetMachineWorkStatus("254155");
        // var_dump($re);
        // exit();

        
        // $time =date('Y-m-d H:i:s',time());
        //         $data=[
        //             'user_id' => 111,
        //             'status' => 2,
        //             'create_at' => $time,
        //             'update_at' => $time,
        //             'amount' => 123
        //         ];

        // Db::startTrans();
        // try {
        //     $id=Db::table('refund')->insertGetId($data);
        // echo $id;
        
            
        //     // 提交事务
        //     Db::commit();
        // } catch (\Exception $e) {
        //     // 回滚事务
        //     Db::rollback();
        //     return $this->renderError('数据更新异常'.$e); 
        // }

        //$bindcard_old = Db::table('area')->select();
        

		// //return ['data'=>$data,'code'=>1,'message'=>'操作完成'];
        // //echo md5("o7zf-0GZ4b_69ixl3t7Dbx1OGnL0");
        
        // $oldunionid = "oZHyT5wfh0CUeuOQmGg3BSldVgmE";

        // $map=[
        //     ['union_id', '=', $oldunionid]
        // ];
        // $data_old = Db::connect('db_old')->table('user')->where($map)->find();
        // if(empty($data_old)){
        //     return $this->renderError("未找到历史用户信息,请联系管理员");
        // }

        // $map=[
        //     ['union_id', '=', $oldunionid]
        // ];
        // $user = Db::table('user')->where($map)->find();
        // if(empty($user)){
        //     return $this->renderError("新系统用户信息无法匹配,请联系管理员");
        // }

        // $map=[
        //     ['apsid', '=', $data_old["apsid"]]
        // ];
        // $bindcard_old=Db::connect('db_old')->table('bindcard')->field('user_id,card_no, create_at, update_at, apsid, status')->where($map)->select();

        // foreach ($bindcard_old as &$row){
        //     $row["user_id"] =$user['id'];
        // }

        // $uid = $user['id'];
        // Db::startTrans();
        // try 
        // {
        //     Db::table('bindcard')->strict(false)->insertAll($bindcard_old);

        //     $sql="UPDATE `user` as a INNER JOIN  ( SELECT  GROUP_CONCAT(card_no SEPARATOR ',') as cards,user_id from (SELECT DISTINCT user_id,card_no from bindcard  where user_id = $uid ) a ) b  ON a.id = b.user_id SET a.cards = b.cards where a.id = $uid ";
        //     var_dump($sql);
        //     Db::execute($sql);

        //     Db::commit();
        // } catch (\Exception $e) {
        //     // 回滚事务
        //     Db::rollback();
        //     return $this->renderError('数据更新异常! - '.$e);
        // }

        //$re = HttpMachine::GetMachineWorkStatus("254819");

        // Db::startTrans();
        // try 
        // {
        //     Db::table('bindcard')->strict(false)->insertAll($bindcard_old);

        //     Db::table('bindcard')->strict(false)->insertAll($bindcard_old);

        //     Db::commit();
        // } catch (\Exception $e) {
        //     // 回滚事务
        //     Db::rollback();
        //     return $this->renderError('数据更新异常! - '.$e);
        // }
          
        return $this->renderSuccess($re); 
    }
	
}
